package com.smart.parking.utils;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AESUtils {


    /**
     * 加密
     */
    public static String getEncryption(String encodeKey, String ivKey, String data) {
        AES aes = new AES(Mode.CBC, Padding.ZeroPadding,
                new SecretKeySpec(encodeKey.getBytes(), "AES"),
                new IvParameterSpec(ivKey.getBytes()));
        return aes.encryptBase64(data, StandardCharsets.UTF_8);
    }

    /**
     * 解密
     */
    public static String getDecrypt(String encodeKey, String ivKey, String data) {
        AES aes = new AES(Mode.CBC, Padding.ZeroPadding,
                new SecretKeySpec(encodeKey.getBytes(), "AES"),
                new IvParameterSpec(ivKey.getBytes()));
        byte[] decryptDataBase64 = aes.decrypt(data);
        return new String(decryptDataBase64, StandardCharsets.UTF_8);
    }

}
